export const deepClone = (obj: any, hash = new WeakMap()) => {
  let target: any = null;
  console.log("////", typeof obj === "object" || typeof obj === "function");

  // 排除obj为null的特殊情况
  if (obj === null) return null;
  // 解决循环引用，hash表中有的直接返回改对象
  if (hash.has(obj)) return hash.get(obj);

  if (typeof obj === "object" || typeof obj === "function") {
    let result: any;
    const type = Object.prototype.toString.call(obj);
    switch (type) {
      case "[object Array]":
        target = [];
        break;
      case "[object Object]":
        target = {};
        break;
      case "[object Map]":
        result = new Map();
        obj.forEach((value: any, key: any) => {
          result.set(key, value);
        });
        return result;
      case "[object Set]":
        result = new Set();
        obj.forEach((value: any, key: any) => {
          result.add(key, value);
        });

        return result;
      case "[object Date]":
        return new Date();
      default: //正则和函数直接返回
        return obj;
    }
    hash.set(obj, target);
  } else {
    return obj;
  }

  for (const key in obj) {
    if (typeof obj[key] === "object") {
      target[key] = deepClone(obj[key], hash);
    } else {
      target[key] = obj[key];
    }
  }
  return target;
};
